package com.itheima.user.controller;

//import com.alibaba.csp.sentinel.annotation.SentinelResource;
//import com.alibaba.csp.sentinel.slots.block.BlockException;

import com.itheima.user.sentinel.fallback.GloalFallBackHandle;
import com.itheima.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description:
 * @Version: V1.0
 */
@RestController
@RequestMapping("/user")
public class SentinelUserController {

    @Autowired
    UserService userService;


    //资源1
    @GetMapping(value = "/hello1")
    public String hello1() {
        return "Hello Sentinel1";
    }

    //资源2
    @GetMapping(value = "/hello2")
    public String hello2() {
        return "Hello Sentinel2";
    }

    //资源3
    @GetMapping(value = "/hello3")
    public String hello3() {
        userService.dosomething();
        return "Hello Sentinel3";
    }

    //资源4
    @GetMapping(value = "/hello4")
    public String hello4() {
        userService.dosomething();
        return "Hello Sentinel4";
    }

    //资源5
    @GetMapping(value = "/hello5/{flag}")
    public String hello5(@PathVariable("flag") int flag) throws InterruptedException {
        if (flag == 1) {
//            TimeUnit.SECONDS.sleep(3);
            Thread.sleep(3000L);
        }
        return "Hello Sentinel5";
    }

//    //资源6
//    @SentinelResource(value = "hello6",
////            blockHandlerClass = DefaultBlockHandle.class,blockHandler="block",
//
//           fallbackClass = GloalFallBackHandle.class,fallback = "gfallbackHandle"
//    )
//    @GetMapping(value = "/hello6/{flag}")
//    public String hello6(@PathVariable("flag") int flag) {
//        if (flag == 1){
//            int i =1/0;
//        }
//        return "Hello Sentinel6";
//    }
//
//    public String fallbackHandle(@PathVariable("flag") int flag) {
//        return "异常服务，返回值";
//    }
//
//    @SentinelResource("hello66")  // *** 热点参数限流必须要使用 @SentinelResource
//    @GetMapping(value = "/hello66")
//    public String hello66(
//            @RequestParam(required = false) String a,
//            @RequestParam(required = false)  Integer b) {
//        return "Hello Sentinel66 a="+a+" b="+b;
//    }
//
//
//    @SentinelResource(value = "hello7", blockHandler="blockHandler123")
//    @GetMapping("hello7")
//    public String hello7(@RequestParam("flag") int flag){  //?
//       // int i =1/0;
//        return "Hello Sentinel7";
//    }
//
//
//    public String blockHandler123(int flag, BlockException bk) {
//        bk.printStackTrace();
//        return "blockHandler 服务异常，稍后重试...." + flag;
//    }

}
